<template>
  <ex-dialog width="50%" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="145px" class="demo-ruleForm">
        
        <el-form-item label="中奖用户：" prop="customerPhone">
          <el-input v-model="ruleForm.customerPhone" placeholder="请输入手机号码" style="width:280px"></el-input>
        </el-form-item>
        <el-form-item label="开奖阶段：" prop="jackpotId" >
          <el-select v-model="ruleForm.jackpotId" placeholder="请选择" filterable style="width:280px" @change="changeJackpotId" :disabled="pathType == 'edit'">
            <el-option v-for="item in stageList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="奖项：" prop="jackpotPrizeId">
          <el-select v-model="ruleForm.jackpotPrizeId" placeholder="请选择" filterable style="width:280px" :disabled="pathType == 'edit'">
            <el-option v-for="item in stagePrizeList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      
      <el-button type="primary"  @click="submitForm('ruleForm')" v-if="id!=null">保存</el-button>
      <el-button type="primary"  @click="handleCancel('ruleForm')" v-if="id==null">取消</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
  // components
  import Loading from "@/components/Loading/index";
  // api
  import {
    stageList,
    stagePrizeList,
    addJackpotCustomer,
    jackpotRecord,
    editCustomerModify,
  } from "@/api/blindBox/activityDetail";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
      listId: String,
      pathType: String,
    },
    data() {
      return {
        title:'添加开奖名单',
        loading: false,
        stageList: [],
        stagePrizeList: [],
        ruleForm: {
          customerPhone: '', //图片url
          jackpotId:"",//奖池阶段id 
          jackpotPrizeId:"",//奖池奖项id 
        },
        rules: {
          customerPhone: [{
            required: true,
            message: "请输入名称",
            trigger: ["blur","change"]
          }],
          jackpotId: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          jackpotPrizeId: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
        },
      };
    },
    computed: {},
    created() {
      this.getstageList()
      if(this.pathType == 'edit'){
        this.getDetail()
      }
    },
    methods: {
      //获取开奖详情
      getDetail(){
        jackpotRecord(this.listId).then(response => {
          console.log('response',response)
          this.ruleForm =  response.result
          this.getstagePrizeList(response.result.jackpotId)
        });
      },
      //开奖阶段选择列表
      getstageList() {
        stageList(this.id).then(response => {
          this.stageList = response.result
         
        });
      },
      //开奖奖项选择列表
      getstagePrizeList(stageId) {
        console.log('jackpotId',stageId)
        stagePrizeList(stageId).then(response => {
          this.stagePrizeList = response.result
        });
      },
      changeJackpotId(e){
        this.getstagePrizeList(e)
        this.ruleForm.jackpotPrizeId = ''
      },
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
    
      imgSuccess(res, file) { //文件上传成功时
        console.log(res.result.url)
        this.ruleForm.imgUrl = res.result.url
        this.$refs.ruleForm.validateField('imgUrl')
        this.imgLoading = false
      },
      onProgress(file) { //文件上传时
        this.imgLoading = true
      },
      onError(file) {
        this.imgLoading = false
      },
      handleCancel(){
        this.handleClose()
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.pathType == 'add'){
              var params = {
                ...this.ruleForm,
              };
              addJackpotCustomer(params).then((res) => {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                }, 1000);
              })
              .finally(() => {

              });
            }else if(this.pathType == 'edit'){
              var params = {
                id: this.listId,
                customerPhone: this.ruleForm.customerPhone
              };
              editCustomerModify(params).then((res) => {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                }, 1000);
              })
              .finally(() => {

              });
            }
            

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
     
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .main {
    padding: 20px 20px;
  }

  .from_box {
    display: flex;
    margin-bottom: 20px;
  }

  .from_box_l {
    width: 110px;
    color: #606266;
    font-size: 14px;
    font-weight: bold;
  }

  .from_box_r {
    color: #333;
    font-size: 15px;
    flex: 1;
  }

  .el_table_box {
    padding: 0 20px 40px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    margin-bottom: 20px;
  }

  .el_table_box_h {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #f3f3f3;
    margin-bottom: 14px;
  }

  .el_table_box_h_title {
    border-left: 4px solid #ff9300;
    padding-left: 4px;
  }

  .carousel_box {
    width: 400px;
    margin-bottom: 20px;
  }

  .swiper_img {
    width: 100%;
    height: 100%;
  }

  .carousel_box_two {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
  }

  .phone_box {
    width: 100px;
    height: 100px;
    margin: 0 10px 10px 0;
  }

  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

  ::v-deep .avatar-uploader2 .el-upload {
    width: 100% !important;
  }

  .avatar-uploader2 {
    width: 228px;
    height: 128px;
    text-align: center;
    line-height: 128px;
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
  }

  .banner_img {
    width: 228px;
    height: 128px;
  }

</style>
